﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Utilities;
using System;
using System.IO;

namespace ACC.Extensions.PCClient.Helper
{
    public class ScriptHelper
    {
        /// <summary>
        /// 从_script文件夹下读取脚本进行编译并生成一个AccProcess的示例
        /// </summary>
        /// <returns></returns>
        public static AccProcess CreateAccProcess()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "_script";
            //检查_script文件夹是否存在
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return null;
            }

            string fileName = String.Format("{0}\\Script.AccProcessEx.cs", path);
            string script = FileHelper.ReadFile(fileName);
            if (String.IsNullOrEmpty(script))
            {
                return null;
            }

            try
            {
                Type type = CompileHelper.CompileScript(script, "Script.AccProcessEx", true);
                AccProcess accProcess = (AccProcess)Activator.CreateInstance(type, null);
                return accProcess;
            }
            catch (Exception ex)
            {
                Log4Helper.Error("Compile AccProcess script error: ", ex);
                return null;
            }
        }
    }
}
